<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bigbox {
            border: 1px solid gray;
            width: 400px;
            height: 500px;
            text-align: center;
            border-radius: 10px;
        }

        .bigbox .title {
            font-size: 16px;
            line-height: 60px;
            text-align: center;
        }

        .bigbox .block {
            margin: 30px 0;
        }

        .bigbox .block input {
            height: 40px;
            width: 80%;
            /* padding-left: 20px; */
            border: 1px solid gray;
            border-radius: 10px;
            outline: none;
            box-sizing: border-box;
        }

        .bigbox :nth-child(5) input {
            color: white;
            background-color: rgb(0, 123, 255);
        }
    </style>
</head>

<body>
    <div class="bigbox">
        <div class="title">注册新账号</div>
        <div class="block">
            <input type="text" placeholder="请输入账号">
        </div>
        <div class="block">
            <input type="password" placeholder="请输入密码">
        </div>
        <div class="block">
            <input type="password" placeholder="确认密码">
        </div>
        <div class="block">
            <input type="button" value="注册">
        </div>
    </div>
</body>

</html>
<script>
    // 对象转查询字符串
    function objToSearchString(obj) {
        var str = ''
        for (var key in obj) {
            str += `${key}=${obj[key]}&`
        }
        return str.slice(0, str.length - 1)
    }
    // 封装ajax函数
    function ajax(method, url, params, cb) {
        // 创建请求对象
        var xhr = new XMLHttpRequest()
        // 配置请求对象
        xhr.open(method, method == 'get' ? url + "?" + objToSearchString(params) : url)
        // 设置回调函数
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                cb(JSON.parse(xhr.responseText))
            }
        }
        // 发送请求
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
        xhr.send(method == 'get' ? null : objToSearchString(params))
    }
    //获取input输入框
    var inputs = document.querySelectorAll('input')
    // console.log(inputs);
    inputs[3].onclick = () => {
        if (inputs[0].value && inputs[1].value) {
            if (/^\d{4}$/.test(inputs[1].value)) {
                if (inputs[1].value == inputs[2].value) {
                    ajax('get', 'https://yantianfeng.com/api/register', {
                        username: inputs[0].value,
                        password: inputs[1].value
                    }, function (data) {
                        console.log(data);
                    })
                } else {
                    alert('密码输入不一致')
                }
            }
            else {
                alert('密码输入有误')
            }
        }
        else {
            alert('用户名/密码不能为空')
        }
    }


</script>